package light.im.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import light.im.IAction;
import light.im.IMEndpoint;
import light.im.IResponseHandler;
import light.im.client.model.ChatSession;
import org.json.JSONException;
import org.json.JSONObject;
import light.network.object.PackObject;

public class IMClient {

	IMEndpoint ep;
	String sessionId;
	String userId;
	
	ClientUI clientUI;
	InetSocketAddress serverAddress;
	
	Map<String, ChatSession> chatSessions = new HashMap<String, ChatSession>();
	
	public void connectServer(String rmHost, int rmPort) throws IOException
	{
		ep = new IMEndpoint(rmHost, rmPort);
		
		ep.start();
		serverAddress = new InetSocketAddress(rmHost, rmPort);
	}
	
	public boolean register(String userId, String psw, String nick) throws IOException
	{
		JSONObject h = new JSONObject();
		final PackObject req = new PackObject(h);
		try {
			h.put("action", "register");
			h.put("userId", userId);
			h.put("password", psw);
			h.put("nickName", nick);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ep.request(serverAddress, req, new IResponseHandler() {
			
			@Override
			public void onSuccess(PackObject resp) {
				clientUI.onRegister(resp);
			}
			@Override
			public void onFail(Throwable t) {
				t.printStackTrace();
			}
		});
		return false;
	}
	
	public void createChat(String userId, String answerAction, IAction handler) throws JSONException
	{
		JSONObject h = new JSONObject();
		PackObject req = new PackObject(h);
		h.put("action", "reqChat");
		h.put("userId", userId);
		ep.request(serverAddress, req, new IResponseHandler() {
			@Override
			public void onSuccess(PackObject resp) {
				clientUI.onCreateChatAnswer(resp);
			}
			@Override
			public void onFail(Throwable t) {
				t.printStackTrace();
			}
		});
	}
	
//	public boolean sendMessage(String userId, String msg) throws IOException
//	{
//		JSONObject h = new JSONObject();
//		try {
//			h.put("action", "sendmsg");
//			h.put("msg", msg);
//			h.put("target", userId);
//			//PackObject req = new PackObject(h);
//			connection.openSession(chatSessions.get(userId).getTargetAddress(), null, null);
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//		return false;
//	}
	
	public boolean login(String userId, String psw) throws IOException
	{
		JSONObject h = new JSONObject();
		try {
			h.put("action", "login");
			h.put("user", userId);
			h.put("psw", psw);
			PackObject req = new PackObject(h);
			ep.request(serverAddress, req, new IResponseHandler() {
				@Override
				public void onSuccess(PackObject resp) {
					sessionId = resp.getString("session");
				}
				@Override
				public void onFail(Throwable t) {
					t.printStackTrace();
				}
			});
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void getFriendsCatalog() throws JSONException
	{
	    JSONObject h = new JSONObject();
	    h.put("action", "friends");
	    PackObject req = new PackObject(h);
	    ep.request(serverAddress, req, new IResponseHandler() {
			@Override
			public void onSuccess(PackObject resp) {
				clientUI.onFriensList(resp);
			}
			@Override
			public void onFail(Throwable t) {
				t.printStackTrace();
			}
		});
	}
	
	public ClientUI getClientUI() {
		return clientUI;
	}

	public void setClientUI(ClientUI clientUI) {
		this.clientUI = clientUI;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
	    IMClient client = new IMClient();
	    client.connectServer("localhost", 10606);
	    //client.register("ruby", "123", "Ruby");
	    client.login("r", "123");;
	}

}
